//index.js
//获取应用实例
const common = require("../../public.js");

Page({
  data: {
    realName: '',
    cardNo: '',
    captchaId: 'fb6455d8a495496f8780b3c0aed5ea85'
  },
  onLoad(options) {
    const that = this
    if (options.backUrl) {
      that.setData({
        backUrl: decodeURIComponent(options.backUrl)
      }, () => {
        console.log(that.data.backUrl,"测试数据")
        console.log(that.data.backUrl, "5657855226backUrlbackUrl85423224")
      })
    }
    wx.getStorage({
      key: 'user',
      success: function (res) {
        that.setData({
          userInfo: res.data.user,
          token: res.data.token,
          openid: res.data.openid,
        })
      },
      fail: function (res) {
        common.showToast('没有用户信息')
      }
    })
  },
  inputValue(e) {},
  sureSubmit() {
    if (common.isEmptyString(this.data.realName)) {
      common.showToast('请输入姓名')
      return
    }
    if (common.isEmptyString(this.data.icardNo)) {
      common.showToast('请输入身份证号')
      return
    }
    // this.selectComponent('#captcha').popup() //验证码登录
    this.submit()
  },
  // // 验证码验证结果回调
  handlerVerify_normal: function (ev) {
    var that = this
    console.log('ev:', ev)
    if (!ev.detail[0]) {
      // 验证成功
      console.log('validate:', ev.detail[1])
    //   this.submit(ev.detail[1])
    } else {
      // 验证失败
      wx.showToast({
        title: '验证失败',
        icon: 'success',
        duration: 2000
      })
    }
  },
  // 返回指定界面
  popPage() {
    const that = this
    const pages = getCurrentPages()
    const prevPage = pages[pages.length - 2] // 返会上个界面
    console.log(that.data.backUrl, "测试数据")
    if (that.data.backUrl) {
    } else {
      if (prevPage) {
        prevPage.onLoad()
      }
      wx.navigateBack({ // 返会上个界面
        delta: 1
      })
    }
  },
  cancel() {
    this.popPage()
  },
  submit(validateCode) {
    const that = this
    const pages = getCurrentPages()
    const prevPage = pages[pages.length - 2] // 返会上个界面
    console.log(that.data.backUrl, "测试数据")
    const tempJson = {
      realName: that.data.realName,
      cardNo: that.data.icardNo,
      userId: that.data.userInfo.id,
      token: that.data.token,
    //   validateCode: validateCode
    }
    // common.post('/user/verify', tempJson, function (res) {
    common.post('/user/verifyUseValidate', tempJson, function (res) {
      console.log(res.msg)
      if (res.status === 200) {
        wx.showToast({
          title: '认证成功',
          success: function () {
            common.loginFunc(that.data.backUrl, validateCode).then(res => {
              console.log(res,"resssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss")
              if (res) {
                wx.removeStorageSync('user')
                common.showLoginPage(null, that.data.backUrl)
                // wx.redirectTo({
                //   url: that.data.backUrl,
                // })
              }
            })
          }
        })
      } else {
  
        common.showRequestErrMsg(res)
        if (res.msg == '实名失败,该用户已经实名') {
          wx.removeStorageSync('user')
          common.showLoginPage(null, that.data.backUrl)
        }
      }
    }, _ => {
      that.selectComponent('#captcha').reset()
    })
  }
})